﻿using System;
using System.Linq;
using System.ComponentModel;

namespace InnovTech.Util.Resources
{

    public class ResourceEntityValueChangedEventArgs: CancelEventArgs
    {

        public object OldValue;
        public object NewValue;

        public ResourceEntityValueChangedEventArgs(object oldValue, object newValue)
        {
            OldValue = oldValue;
            NewValue = newValue;
        }
    }

    /// <summary>
    /// a instance of this class contains all informations of a resource entity (key-value pair)
    /// </summary>
    public class ResxEntity
    {

        public event EventHandler<ResourceEntityValueChangedEventArgs> ValueChanged;

        #region fields

        /// <summary>
        /// comment of this resource entity
        /// </summary>
        private string _comment;

        /// <summary>
        /// the key or name of this resource entity
        /// </summary>
        private string _key;

        /// <summary>
        /// the value of this resource entity
        /// </summary>
        private object _value;
        

        #endregion

        #region Properties

        /// <summary>
        /// get or set comment of this resource entity
        /// </summary>
        public string Comment
        {
            get
            {
                if (_comment == null)
                    return string.Empty;
                return _comment;
            }
            set
            {
                _comment = value;
            }
        }


        /// <summary>
        /// indicate whether this resource entity contains a comment
        /// </summary>
        public bool IsCommentExists { get { return !string.IsNullOrEmpty(Comment.Trim()); } }

        /// <summary>
        /// get or set the key or name of this resource entity
        /// </summary>
        public string Key
        {
            get { return _key; }
            set
            {
                _key = value;
            }
        }

        /// <summary>
        /// only be setted at time for import  
        /// </summary>
        public DateTime LastUpdate { get; set; }

        /// <summary>
        /// get or set the value of this resource entity
        /// </summary>
        public object Value
        {
            get
            {
                return _value;
            }
            set
            {
                if (_value != value)
                {
                    object oldValue = _value;
                    _value = value;
                    OnValueChanged(oldValue);
                }
            }
        }


        #endregion

        #region methods

        private void OnValueChanged(object oldValue)
        {
            if (ValueChanged != null)
            {
                ValueChanged(this, new ResourceEntityValueChangedEventArgs(oldValue, Value));
            }
        }

        #endregion methods

    }
}
